package com.atguigu.jxc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.PurchaseListService;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @program: jxc
 * @description: 供应商统计统计报表
 * @author: ZJH
 * @create: 2023-02-11 09:44
 **/
@RestController
@RequestMapping("/purchaseListGoods")
public class PurchaseListController {


    @Autowired
    private PurchaseListService purchaseListService;
    /**
     *   //请求URL：http://localhost:8080/purchaseListGoods/updateState
     *   进货统计（可根据 商品类别、商品编码或名称 条件查询）
     */
    @PostMapping("/updateState")
    public ServiceVO updateState(Integer purchaseListId){
       purchaseListService.updateState(purchaseListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    //请求URL：http://localhost:8080/purchaseListGoods/count
    @PostMapping("/count")
    public String count(String sTime, String eTime ,
                        Integer goodsTypeId, String codeOrName){
        List<Map<String, Object>> count = purchaseListService.count(sTime, eTime, goodsTypeId, codeOrName);
        return JSON.toJSONString(count);
    }
    /**
     * 请求URL：/purchaseListGoods/save?purchaseNumber=JH1605768306735
     *purchaseListGoods/save?purchaseNumber=JH1676095318769
     */
    @PostMapping("/save")
public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr, HttpServletRequest request){
        String purchaseNumber = request.getParameter("purchaseNumber");
        User currentUser = (User) request.getSession().getAttribute("currentUser");
        Integer userid = currentUser.getUserId();
        purchaseList.setPurchaseNumber(purchaseNumber);
        purchaseList.setUserId(userid);
        Gson gson = new Gson();
        List<PurchaseListGoods> purchaseListGoodsList =
                gson.fromJson(purchaseListGoodsStr,new TypeToken<List<PurchaseListGoods>>(){}.getType());
        for (PurchaseListGoods purchaseListGoods : purchaseListGoodsList) {
            purchaseListGoods.setPurchaseListId(purchaseListGoods.getPurchaseListId());
            purchaseListService.insert(purchaseList,purchaseListGoods);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
}